package com.cmsky.open.food.controller.admin;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.org.rapid_framework.web.scope.Flash;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.cmsky.open.base.manager.AreaInfoManager;
import com.cmsky.open.base.model.AreaInfo;
import com.cmsky.open.core.base.BasePager;
import com.cmsky.open.core.util.JsonUtils;
import com.cmsky.open.core.util.PagerUitl;

import cn.org.rapid_framework.beanutils.BeanUtils;

import java.util.*;

import com.cmsky.open.core.base.*;
import com.cmsky.open.core.util.*;

import cn.org.rapid_framework.util.*;
import cn.org.rapid_framework.web.util.*;
import cn.org.rapid_framework.page.*;
import cn.org.rapid_framework.page.impl.*;

import com.cmsky.open.food.model.*;
import com.cmsky.open.food.dao.*;
import com.cmsky.open.food.manager.*;
import com.cmsky.open.food.vo.query.*;
import com.cmsky.open.shop.manager.ShopAreaInfoManager;
import com.cmsky.open.shop.manager.ShopInfoManager;
import com.cmsky.open.shop.model.ShopAreaInfo;
import com.cmsky.open.shop.vo.query.ShopAreaInfoQuery;
import com.cmsky.open.shop.vo.query.ShopInfoQuery;

/**
 * @author jandyfish email:jandyfish(a)gmail.com
 * @version 1.0
 * @since 1.0
 */


@Controller("admin.food.foodAreaInfo")
@RequestMapping("/admin/food/foodAreaInfo")
public class FoodAreaInfoController extends BaseSpringController{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	private AreaInfoManager areaInfoManager;
	private FoodAreaInfoManager foodAreaInfoManager;
	private FoodInfoManager foodInfoManager;
	private ShopInfoManager shopInfoManager;
	private ShopAreaInfoManager shopAreaInfoManager;
	
	private final String LIST_ACTION = "redirect:/admin/food/foodAreaInfo/list.do";
	
	public FoodAreaInfoController() {
	}
	
	/** 
	 * 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写
	 **/
	public void setFoodAreaInfoManager(FoodAreaInfoManager manager) {
		this.foodAreaInfoManager = manager;
	}

	public void setAreaInfoManager(AreaInfoManager areaInfoManager) {
		this.areaInfoManager = areaInfoManager;
	}

	public void setFoodInfoManager(FoodInfoManager foodInfoManager) {
		this.foodInfoManager = foodInfoManager;
	}

	public void setShopInfoManager(ShopInfoManager shopInfoManager) {
		this.shopInfoManager = shopInfoManager;
	}

	public void setShopAreaInfoManager(ShopAreaInfoManager shopAreaInfoManager) {
		this.shopAreaInfoManager = shopAreaInfoManager;
	}

	/**
	 * 增加了@ModelAttribute的方法可以在本controller的方法调用前执行,可以存放一些共享变量,如枚举值
	 */
	@ModelAttribute
	public void init(ModelMap model) {
		model.put("now", new java.sql.Timestamp(System.currentTimeMillis()));
	}
	
	/** 
	 * 执行搜索 
	 **/
	@RequestMapping("list")
	public ModelAndView list(HttpServletRequest request,HttpServletResponse response,FoodAreaInfoQuery query) {
		BasePager page = this.foodAreaInfoManager.findPage(query);
		
		ModelAndView result = new ModelAndView("/admin/food/foodAreaInfo/list");
		result.addAllObjects(toModelMap(page, query));
		return result;
	}
	
	/**
	 * 默认管理页
	 */
	@RequestMapping("/index")
	public ModelAndView index(HttpServletRequest request,HttpServletResponse response){
		return new ModelAndView("/admin/food/foodAreaInfo/index");
	}
	/**
	 * ajax搜索
	 * @param request
	 * @param response
	 * @param query
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/ajaxList")
	public String ajaxList(HttpServletRequest request,HttpServletResponse response,FoodAreaInfoQuery query) {
		BasePager page = this.foodAreaInfoManager.findPage(query);
		return JsonUtils.jsonFromObject(PagerUitl.pasreToLigerMap(page));
	}
	/**
	 * 批量更新或新增---设置单个商品发布的社区列表
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/saveOrUpdate")
	public String saveOrUpdate(HttpServletRequest request,HttpServletResponse response){
		Map<String,Object> result = null;
		String json = request.getParameter("json");
		Integer foodId = Integer.parseInt(request.getParameter("foodId"));
		FoodInfo foodInfo = this.foodInfoManager.getById(foodId);
		if(null == foodInfo){
			result = ErrorMapUtil.createErrorMap("404", "没有相应的快餐信息!");
		}else{
			List tempList = JsonUtils.listFromJson(json);
			List<FoodAreaInfo> infoList = new LinkedList<FoodAreaInfo>();
			ShopAreaInfoQuery saQuery = new ShopAreaInfoQuery();
			FoodInfoQuery fiQuery = new FoodInfoQuery();
			saQuery.setShopId(foodInfo.getShopId());
			saQuery.setGoodsClassifyId(foodInfo.getType());
			fiQuery.setState(1);
			for(Object obj : tempList){
				FoodAreaInfo bean = JsonUtils.objectFromJson(JsonUtils.jsonFromObject(obj),FoodAreaInfo.class);
				if(bean.getFoodId()!=foodInfo.getId()){
					continue;
				}
				saQuery.setAreaId(bean.getAreaId());
				List<ShopAreaInfo> saList = this.shopAreaInfoManager.findPage(saQuery).getRows();
				if(1!=saList.size()){
					result = ErrorMapUtil.createErrorMap("500", "商户-社区有误!");
					break;
				}
				int maxCount = saList.get(0).getNum();
				
				fiQuery.setType(foodInfo.getType());
				fiQuery.setAreaId(bean.getAreaId());
				int realCount = this.foodInfoManager.countByKey(fiQuery);//商品类型在当前社区的上架商品数
				if(maxCount>realCount){
					infoList.add(bean);
				}else{
					AreaInfo areaInfo = this.areaInfoManager.getById(bean.getAreaId());
					result = ErrorMapUtil.createErrorMap("500", "社区\""+areaInfo.getName()+"\"已经达到了"+maxCount+"数量,不能设置!");
					break;
				}
			}
			if(result==null){
				Integer count = this.foodAreaInfoManager.saveOrUpdate(infoList, foodInfo.getId());
				result = new HashMap<String,Object>();
				result.put("code", "0");
				result.put("num", count);
			}
		}
		return JsonUtils.jsonFromObject(result);
	}
	
	/** 
	 * 查看对象
	 **/
	@RequestMapping("show")
	public ModelAndView show(HttpServletRequest request,HttpServletResponse response) throws Exception {
		java.lang.Integer id = new java.lang.Integer(request.getParameter("id"));
		FoodAreaInfo foodAreaInfo = (FoodAreaInfo)foodAreaInfoManager.getById(id);
		return new ModelAndView("/admin/food/foodAreaInfo/show","foodAreaInfo",foodAreaInfo);
	}
	
	/** 
	 * 进入新增页面
	 **/
	@RequestMapping("create")
	public ModelAndView create(HttpServletRequest request,HttpServletResponse response,FoodAreaInfo foodAreaInfo) throws Exception {
		return new ModelAndView("/admin/food/foodAreaInfo/create","foodAreaInfo",foodAreaInfo);
	}
	
	/** 
	 * 保存新增对象
	 **/
	@RequestMapping("save")
	public ModelAndView save(HttpServletRequest request,HttpServletResponse response,FoodAreaInfo foodAreaInfo) throws Exception {
		foodAreaInfoManager.save(foodAreaInfo);
		Flash.current().success(CREATED_SUCCESS); //存放在Flash中的数据,在下一次http请求中仍然可以读取数据,error()用于显示错误消息
		return new ModelAndView(LIST_ACTION);
	}
	
	/**
	 * 进入更新页面
	 **/
	@RequestMapping("edit")
	public ModelAndView edit(HttpServletRequest request,HttpServletResponse response) throws Exception {
		java.lang.Integer id = new java.lang.Integer(request.getParameter("id"));
		FoodAreaInfo foodAreaInfo = (FoodAreaInfo)foodAreaInfoManager.getById(id);
		return new ModelAndView("/admin/food/foodAreaInfo/edit","foodAreaInfo",foodAreaInfo);
	}
	
	/**
	 * 保存更新对象
	 **/
	@RequestMapping("update")
	public ModelAndView update(HttpServletRequest request,HttpServletResponse response) throws Exception {
		java.lang.Integer id = new java.lang.Integer(request.getParameter("id"));
		
		FoodAreaInfo foodAreaInfo = (FoodAreaInfo)foodAreaInfoManager.getById(id);
		bind(request,foodAreaInfo);
		foodAreaInfoManager.update(foodAreaInfo);
		Flash.current().success(UPDATE_SUCCESS);
		return new ModelAndView(LIST_ACTION);
	}
	
	/**
	 *删除对象
	 **/
	@RequestMapping("delete")
	public ModelAndView delete(HttpServletRequest request,HttpServletResponse response) {
		String[] items = request.getParameterValues("items");
		for(int i = 0; i < items.length; i++) {
			Hashtable params = HttpUtils.parseQueryString(items[i]);
			
			java.lang.Integer id = new java.lang.Integer((String)params.get("id"));
			
			foodAreaInfoManager.removeById(id);
		}
		Flash.current().success(DELETE_SUCCESS);
		return new ModelAndView(LIST_ACTION);
	}
	
}

